//
//  FriednTravelDetailViewController.m
//  ShareNavigation
//
//  Created by 水木 傅 on 12-5-18.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "FriendTravelDetailViewController.h"
#import "ASIHTTPRequest.h"
#import "SingletonFriendInfo.h"

@implementation FriendTravelDetailViewController
@synthesize headPhoto;
@synthesize UserID;
@synthesize Location;
@synthesize Time;
@synthesize travelSignature;
@synthesize signInPicture;
@synthesize UUID;
@synthesize friendTravelInfo;

-(void)viewDidLoad
{
    [super viewDidLoad];
    if(!friendTravelInfo)
    {
        friendTravelInfo = [[FriendTravelInfo alloc] init];
        NSLog(@"detail null");
    }
   // self.friendTravelInfo.UUID = [SingletonFriendInfo shareManager].UUID;
    self.friendTravelInfo.headPhoto = [SingletonFriendInfo shareManager].headPhoto;
    self.friendTravelInfo.userID = [SingletonFriendInfo shareManager].userID;
    self.friendTravelInfo.signInTime = [SingletonFriendInfo shareManager].signInTime;
    self.friendTravelInfo.travelSignature = [SingletonFriendInfo shareManager].travelSignature;
    self.friendTravelInfo.location = [SingletonFriendInfo shareManager].location;
    self.friendTravelInfo.hasImage = [SingletonFriendInfo shareManager].hasImage;
    if([SingletonFriendInfo shareManager].hasImage)
    {
        NSLog(@"TRUE");
    }
    else
    {
        NSLog(@"FALSE");
    }
    [self setData:[SingletonFriendInfo shareManager]];
    NSLog(@"---++--%@",friendTravelInfo.UUID);
}

- (void)viewDidUnload {
    [self setUUID:nil];
    [self setHeadPhoto:nil];
    [self setUserID:nil];
    [self setLocation:nil];
    [self setTime:nil];
    [self setTravelSignature:nil];
    [self setSignInPicture:nil];
    [super viewDidUnload];
}

-(void)setData:(SingletonFriendInfo *)info
{
    if([info.headPhoto isEqualToString:@""])
    {
        self.headPhoto.image = [UIImage imageNamed:@"null.jpg"];
    }
    else
    {
        self.headPhoto.image = [self getPicture:info.headPhoto];
    }
    self.UserID.text = info.userID;
    if(![info.location isEqualToString:@""])
    {
        self.Location.text = [NSString stringWithFormat:@"在%@",info.location];
    }
    else 
    {
        self.Location.text = @"";
    }
    self.Time.text = info.signInTime;
    self.travelSignature.text  = info.travelSignature;
    if(info.hasImage)
    {
        self.signInPicture.image = [self getPicture:info.UUID];
    }
    else
    {
        self.signInPicture.image = [UIImage imageNamed:@"null.jpg"];
    }
}

//---  通过名称获取图片  ----
-(UIImage *) getPicture :(NSString *)imageName
{
    NSString *urlStr = [NSString stringWithFormat:@"http://localhost/~walker/ShareNavigationSVN/trunk/ShareNavigationPHP/upload/%@.jpg",imageName];
    NSURL *url  =[NSURL URLWithString: urlStr];
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 
                                                         NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    
    NSString *archivePath = [documentsDirectory 
                             stringByAppendingPathComponent:
                             [NSString stringWithFormat:@"%@.jpg",imageName]];
    
    [request setDownloadDestinationPath:archivePath];
    return [UIImage imageWithContentsOfFile:archivePath];
}
@end
